package com.hq.ims.data.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hq.ims.data.config.verification.VdAdd;
import com.hq.ims.data.config.verification.VdEdit;
import com.hq.ims.data.dao.BusiChangeTimeRecordMapper;
import com.hq.ims.data.entity.BusiChangeTimeRecord;
import com.hq.ims.data.vo.BusiChangeTimeRecordMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.utils.util.ObjectUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 蒋柏平
 * @detail 经营信息变更历史记录 服务实现类
 * @since 20241025
 */
@Service
public class BaseBusiChangeTimeRecordService extends BaseService<BusiChangeTimeRecordMapper, BusiChangeTimeRecord> {


    /**
     * 分页
     */
    public List<BusiChangeTimeRecordMapstruct.BusiChangeTimeRecordVo> getByPage(
            RequestPage page,
            BusiChangeTimeRecordMapstruct.PageSearchVo searchVo
    ) {
        //验证
        LambdaQueryChainWrapper<BusiChangeTimeRecord> lq = this.lambdaQuery();
        //排序
        //查询条件

        //执行分页
        lq.page(page);

        List<BusiChangeTimeRecord> dataList = page.getRecords();
        List<BusiChangeTimeRecordMapstruct.BusiChangeTimeRecordVo> voList = BusiChangeTimeRecordMapstruct.INSTANCE.toBusiChangeTimeRecordVoList(dataList);
        page.setRecords(voList);
        return voList;
    }

    /**
     * 添加
     */
    @Transactional(rollbackFor = Exception.class)
    public BusiChangeTimeRecord addData(BusiChangeTimeRecordMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdAdd.class);

        //账值新的对象
        BusiChangeTimeRecord entity = new BusiChangeTimeRecord();
        entity.setChangeTime(vo.getChangeTime());
        entity.setChangeBeforeId(vo.getChangeBeforeId());
        entity.setChangeAfterId(vo.getChangeAfterId());
        entity.setBusiInfoId(vo.getBusiInfoId());
        entity.setAuditStatus(vo.getAuditStatus());
        entity.insert();
        return entity;
    }

    /**
     * 编辑
     */
    @Transactional(rollbackFor = Exception.class)
    public BusiChangeTimeRecord editData(BusiChangeTimeRecordMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdEdit.class);

        //账值新的对象
        BusiChangeTimeRecord entity = this.getById(vo.getBusiChangeTimeRecordId());
        ObjectUtils.isNullThrow(entity, "数据不存在");

        entity.setChangeTime(vo.getChangeTime());
        entity.setChangeBeforeId(vo.getChangeBeforeId());
        entity.setChangeAfterId(vo.getChangeAfterId());
        entity.setAuditStatus(vo.getAuditStatus());
        entity.updateById();
        return entity;
    }


    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void delData(String busiChangeTimeRecordId) {
        BusiChangeTimeRecord data = this.getById(busiChangeTimeRecordId);
        if (data == null) {
            return;
        }
        data.deleteById();
    }

    /**
     * 获取原始数据详情
     */
    public BusiChangeTimeRecordMapstruct.BusiChangeTimeRecordVo getDetailVo(String busiChangeTimeRecordId) {
        BusiChangeTimeRecord entity = this.getById(busiChangeTimeRecordId);
        BusiChangeTimeRecordMapstruct.BusiChangeTimeRecordVo vo = BusiChangeTimeRecordMapstruct.INSTANCE.toBusiChangeTimeRecordVo(entity);
        return vo;
    }

    /**
     * 查询变更历史记录
     */
    public List<BusiChangeTimeRecordMapstruct.BusiChangeTimeRecordVo> getChangeData(String busiInfoId) {
        List<BusiChangeTimeRecord> list = this.lambdaQuery()
                .eq(BusiChangeTimeRecord::getBusiInfoId, busiInfoId)
                .list();
        return BusiChangeTimeRecordMapstruct.INSTANCE.toBusiChangeTimeRecordVoList(list);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delByBusiId(String busiId) {
        this.lambdaUpdate().eq(BusiChangeTimeRecord::getBusiInfoId,busiId).remove();
    }
}